local skel = fk.CreateSkill{
  name = "tea__sixiang",
}
local TeaU = require "packages.wild_collection.utility.utility"
Fk:loadTranslationTable{
  ["tea__sixiang"] = "四相",
  [":tea__sixiang"] = "游戏开始时，你摸四张牌并将四张牌以任意顺序置于你武将牌的东南西北方位，称为“风”。当你每回合首次使用一种花色的牌后"..
  "，若“风”中包含此花色且此时为你的回合内/外，你可以顺/逆时针转动“风”。<br><font color='gray'>（东风对应♠、南风对应♥、西风对应♣、北风对应♦）</font>",
  ["@sixiang_wind"] = "",
  ["#tea__sixiang-select"] = "待选",
  ["sixiang_east"] = "东",
  ["sixiang_north"] = "北",
  ["sixiang_west"] = "西",
  ["sixiang_south"] = "南",
  ["@[sixiang]"] = "四相",
  ["#tea__sixiang_pile"] = "风",
  ["#TeaSixiangLog"] = "%from %arg 转动了 %arg2",
  ["tea__sixiang-follow"] = "顺时针",
  ["tea__sixiang-counter"] = "逆时针",
  ["tea__sixiang-invoke"] = "四相：你可以%arg转动“风”",
  ["#tea__sixiang-card"] = "四相：请选择四张牌，将之置为“风”",
}

Fk:addQmlMark{
  name = "sixiang",
  qml_path = "packages/wild_collection/qml/Sixiang",
  how_to_show = function() return " " end,
}
---改变风
---@param player ServerPlayer
---@param order "__follow"|"__counter"
local rollTheWinds = function (player, order)
  local mark = player:getTableMark("tea__sixiang_record")
  local room = player.room
  local new_list = {}
  if #mark ~= 4 then return end
  if not table.contains({"__follow", "__counter"}, order) then return end
  if order == "__follow" then
    new_list[1] = mark[3]
    new_list[4] = mark[1]
    new_list[2] = mark[4]
    new_list[3] = mark[2]
  elseif order == "__counter" then
    new_list[3] = mark[1]
    new_list[1] = mark[4]
    new_list[4] = mark[2]
    new_list[2] = mark[3]
  end
  local _mark = {}
  _mark._tab = new_list
  room:setPlayerMark(player, "@[sixiang]", _mark)
  room:setPlayerMark(player, "tea__sixiang_record", new_list)
  room:sendLog{
    type = "#TeaSixiangLog",
    from = player.id,
    arg = order == "__follow" and "tea__sixiang-follow" or "tea__sixiang-counter",
    arg2 = "#tea__sixiang_pile",
  }
  room.logic:trigger(TeaU.AfterSixiangChanged, player, { target = player, direction = order })
end

---获得语音数字
---@param player any
---@return integer
local getVoice = function (player)
  local isMale = player.phase ~= Player.NotActive
  if isMale then
    return table.random{1, 2}
  else
    return table.random{3, 4}
  end
end

---@type TrigSkelSpec<TrigFunc>
local sixiang_start = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name, "special")
    player:broadcastSkillInvoke(skel.name, getVoice(player))
    player:drawCards(4, skel.name)
    if #player:getCardIds("he") > 3 and not player.dead then
      local cids = room:askToCards(player, {
        min_num = 4,
        max_num = 4,
        skill_name = skel.name,
        include_equip = true,
        cancelable = false,
        prompt = "#tea__sixiang-card",
      })
      if #cids == 4 then
        local data_table = {
          cards = cids,
          ori_cards = {},
          ok_option = {"OK"},
        }
        local qml = "packages/wild_collection/qml/SixiangBox.qml"
        local result = player.room:askToCustomDialog(player, {
          skill_name = skel.name,
          qml_path = qml,
          extra_data = data_table,
          ban_swap = false,
          }
        )
        print(result)
        local ids
        if result ~= "" then
          ids = json.decode(result).cardMap
        else
          ids = table.random(player:getCardIds("he"), 4)
        end
        if #ids == 4 then
          local mark = {}
          mark._tab = ids
          room:setPlayerMark(player, "@[sixiang]", mark)
          room:setPlayerMark(player, "tea__sixiang_record", ids)
          player:addToPile("#tea__sixiang_pile", ids, true, skel.name, player)
        end
      end
    end
  end
}
---@type TrigSkelSpec<UseCardFunc>
local sixiang = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player == target and player:hasSkill(skel.name) and data.card.suit then
      local suit = data.card.suit
      if suit == Card.NoSuit then return end
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      local dict = {}
      if not turn_event then return end
      room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        local dat = e.data ---@type UseCardData
        if dat.from == player then
          local _suit = dat.card.suit
          if not _suit or _suit == Card.NoSuit then return false end
          dict[_suit] = dict[_suit] or {}
          table.insert(dict[_suit], dat.card)
        end
        return false
      end, turn_event.id)
      return #dict[suit] == 1 and table.find(player:getTableMark("tea__sixiang_record"), function(id)
        return Fk:getCardById(id).suit == suit
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local order = player.room.current == player and "tea__sixiang-follow" or "tea__sixiang-counter"
    return player.room:askToSkillInvoke(player, {
      skill_name = skel.name,
      prompt = "tea__sixiang-invoke:::" .. order
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name, "special")
    player:broadcastSkillInvoke(skel.name, getVoice(player))
    local order = room.current == player and "__follow" or "__counter"
    rollTheWinds(player, order)
  end
}
skel:addEffect(fk.GameStart, sixiang_start)
skel:addEffect(fk.CardUseFinished, sixiang)
return skel